﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class EmployeeDAO : IDAO<Employee>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(Employee entity)
        {
            agrDb.Employees.AddObject(entity);
        }

        public void remove(Employee entity)
        {
            agrDb.Employees.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (Employee Employee in agrDb.Employees.Where(s => s.EmployeeCode == code))
            {
                agrDb.Employees.DeleteObject(Employee);
            }
        }

        public void merge(Employee entity)
        {
            foreach (Employee Employee in agrDb.Employees.Where(s => s.EmployeeCode == entity.EmployeeCode))
            {
                Employee.EmployeeFirstName = entity.EmployeeCode;
                Employee.EmployeeGender = entity.EmployeeGender;
                Employee.EmployeeLastName = entity.EmployeeLastName;
                Employee.EmployeePhysicalAddress = entity.EmployeePhysicalAddress;
                Employee.EmployeePostalAddress = entity.EmployeePostalAddress;
                Employee.EmployeeType = entity.EmployeeType;

            }

            //Select ALL
            //var students = (from s in agrDb.Students
            //                where s.StudentCode == "dsfsdf"
            //                select s);

            //Select Certain Columns
            //var students2 = (from s in agrDb.Students
            //                select new
            //                {
            //                    s.StudentCourses,
            //                    s.StudentDisability
            //                });

            //Order rows
            //var orderedListByCourses = students.OrderBy(s => s.StudentCourses);

        }

        public Employee findById(string code)
        {
            Employee Employee = new Employee();

            foreach (Employee stud in agrDb.Employees.Where(s => s.EmployeeCode == code))
            {
                Employee = stud;
            }

            return Employee;
        }

        public IEnumerable<Employee> findAll()
        {
            var Employees = (from stud in agrDb.Employees
                            select stud);

            return Employees;
        }

        public IEnumerable<Employee> findInRange(int firstResult, int maxResuslts)
        {
            var Employees = (from stud in agrDb.Employees
                            select stud);

            return Employees.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var Employees = (from stud in agrDb.Employees
                            select stud).ToList();

            return Employees.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {

            }
        }
    }
}